package com.example.gestioncontacts;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.Node;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.alorm.core.sqlgen.DataBaseSchemaGenerator;
import com.alorm.sqlite.MyDB;

import com.example.BD.ContactsDBAdaptateur;
import com.example.metier.Contact;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AjoutContact extends Activity {

	private Button bAjouter;
	private Button bAnnuler;
	private EditText eNom;
	private EditText ePrenom;
	private EditText eNum;
	public static AjoutContact ana ;
	private static final String TAG2 = AjoutContact.class.getSimpleName();

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ajouter_contact);
		ana=this;

		bAjouter = (Button) findViewById(R.id.ajouter);
		bAnnuler = (Button) findViewById(R.id.annuler);
		eNom = (EditText) findViewById(R.id.edNom);
		ePrenom = (EditText) findViewById(R.id.edPrenom);
		eNum = (EditText) findViewById(R.id.edNum);

		bAjouter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Contact c = new Contact(eNom.getText().toString(), ePrenom
						.getText().toString(), eNum.getText().toString());

				// ContactsDBAdaptateur cda = new
				// ContactsDBAdaptateur(AjoutContact.this);
				// cda.open();
				// cda.insertContact(c);
				// cda.close();

				try {
					Log.d(TAG2, "----> je suis la <------- : ");

					MyDB myDB = MyDB.getMyDB(AjoutContact.this, "contact.db",null, 1);
					myDB.open();
					myDB.crateSchema();
					myDB.save(c);
					myDB.close();

					Log.d(TAG2, "------------------> No Exce <----------------");
				} catch (Exception e) {
					Log.d(TAG2, "--> Exception <-- " + e.getMessage());
				}

				Toast.makeText(AjoutContact.this, c + " a été Ajouté",
						Toast.LENGTH_LONG).show();

				Intent i = new Intent(AjoutContact.this, MainActivity.class);
				startActivity(i);
				finish();

			}
		});

		bAnnuler.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent i = new Intent(AjoutContact.this, MainActivity.class);
				startActivity(i);
				finish();

			}
		});
	}
}
